import std.stdio: writefln;
import derelict.sdl.sdl;
import std.math;

import Utils;
import Pixels;
import Color;


class Blur{
	static float distr[5] = [0.15, 0.5, 1., 0.5, 0.15];

	this(){
	}

	void blur(SDL_Surface *surface){
		uint *p = cast(uint*)surface.pixels;
		static uint newS[xResolution * yResolution];
		uint tmp;
		uint col;
		newS[0..$] = 0;
		for(int j = 0; j < yResolution; j++)
			for(int i = 2; i < xResolution - 2; i++){
				tmp = i+xResolution*j;
//				for(int k = 0; k < 5; k++)
					if((col=p[tmp ]) != background){
						newS[tmp] = (color(p[tmp]) + color(newS[i +xResolution*j])).ui;
						newS[tmp - 1] = (color(newS[tmp - 1])+ color(p[tmp-1]) + color(col)*distr[1]).ui;
						newS[tmp - 2] = (color(newS[tmp - 2])+ color(p[tmp-2]) + color(col)*distr[0]).ui;
						newS[tmp + 1] = (color(newS[tmp + 1])+ color(p[tmp+1]) + color(col)*distr[3]).ui;
						newS[tmp + 2] = (color(newS[tmp + 2])+ color(p[tmp+2]) + color(col)*distr[4]).ui;
					}
			}
		
		for(int i = 0; i < xResolution ; i++)
			for(int j = 2; j < yResolution - 2; j++){
				tmp = i+xResolution*j;
//				for(int k = 0; k < 5; k++)
					if((col=p[tmp ]) != background){
						newS[tmp] = (color(p[tmp]) + color(newS[i +xResolution*j])).ui;
						newS[i+xResolution*(j - 1)] = (color(newS[i+xResolution*(j - 1)])+ color(p[i+xResolution*(j-1)]) + color(col)*distr[1]).ui;
						newS[i+xResolution*(j - 2)] = (color(newS[i+xResolution*(j - 2)])+ color(p[i+xResolution*(j-2)]) + color(col)*distr[0]).ui;
						newS[i+xResolution*(j + 1)] = (color(newS[i+xResolution*(j + 1)])+ color(p[i+xResolution*(j+1)]) + color(col)*distr[3]).ui;
						newS[i+xResolution*(j + 2)] = (color(newS[i+xResolution*(j + 2)])+ color(p[i+xResolution*(j+2)]) + color(col)*distr[4]).ui;
					}
			}


		p[0..xResolution*yResolution] = newS[0..xResolution*yResolution];
	}

}
